Type CB3DMesh

	Field MeshID:Int
	Field B3dMesh:TMesh
	Field CMesh:CalCoreMesh
	Field surf:TSurface[]
	Field filename:String
	Field name:String
	Field isloaded:Byte = False
	Field parent:CB3DModel


	Method PaintMaterial(ii:Int,brush:TBrush)
		'debuglog "Painting Surface"
		surf[ii].PaintSurface(brush)		
	
	End Method
	
	Method Build:Byte(ind:Int,v:Float[],f:Short[])
		
		If CMesh = Null 
			DebugLog "Cmesh was null!"
			Return False
			
		EndIf
		If B3dMesh = Null Then
			B3dMesh = TMesh.CreateMesh(parent.B3dModel)
			'B3dMesh.anim = 2				
		EndIf
		
		If ind > surf.length-1 Then
			surf = surf[..surf.length+1]		
			surf[ind] = B3dMesh.CreateSurface()
			DebugLog "Build: "+ind
		Else
			surf[ind].ClearSurface(True,True)
			DebugLog "ReBuild: "+ind	
		EndIf		


		
		For Local i:Int = 0 To v.length-1 Step 3
			surf[ind].AddVertex(v[i],v[i+1],v[i+2])
		Next
		For Local t:Int = 0 To f.length-1 Step 3
			surf[ind].AddTriangle:Int(f[t],f[t+1],f[t+2])
		Next
		Rem
		Local vas:Int = v.length
		
		surf[ind].no_verts = vas/3
		surf[ind].vert_array_size = vas
		surf[ind].vert_coords = v

		surf[ind].vert_coords=surf[ind].vert_coords[..vas]
		surf[ind].vert_tex_coords0=surf[ind].vert_tex_coords0[..surf[ind].no_verts*2]
		surf[ind].vert_tex_coords1=surf[ind].vert_tex_coords1[..surf[ind].no_verts*2]
		surf[ind].vert_norm=surf[ind].vert_norm[..vas]
		surf[ind].vert_col=surf[ind].vert_col[..surf[ind].no_verts*4]

		Local tas:Int=f.length
		surf[ind].no_tris = tas/3
		
		surf[ind].tris = f
		EndRem
		
		surf[ind].UpdateNormals()
		'surf[ind].reset_vbo:|1|2|16
		isloaded = True
		Return True
	End Method	



	Method Update(si:Int,v:Float[],n:Float[],t:Float[])
		
		surf[si].vert_coords = v
		surf[si].vert_norm = n
		surf[si].vert_tex_coords0 = t
		surf[si].reset_vbo:|1|2|4
	End Method
	
End Type
